<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"
xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=ProgId content=Word.Document>
<meta name=Generator content="Microsoft Word 12">
<meta name=Originator content="Microsoft Word 12">
<link rel=File-List href="threading_files/filelist.xml">
<title>C++Script Manual</title>
<!--[if gte mso 9]><xml>
 <o:DocumentProperties>
  <o:Author>calum</o:Author>
  <o:LastAuthor>calum</o:LastAuthor>
  <o:Revision>8</o:Revision>
  <o:TotalTime>4</o:TotalTime>
  <o:Created>2008-12-18T22:16:00Z</o:Created>
  <o:LastSaved>2008-12-18T22:20:00Z</o:LastSaved>
  <o:Pages>2</o:Pages>
  <o:Words>906</o:Words>
  <o:Characters>5170</o:Characters>
  <o:Lines>43</o:Lines>
  <o:Paragraphs>12</o:Paragraphs>
  <o:CharactersWithSpaces>6064</o:CharactersWithSpaces>
  <o:Version>12.00</o:Version>
 </o:DocumentProperties>
</xml><![endif]-->
<link rel=themeData href="threading_files/themedata.thmx">
<link rel=colorSchemeMapping href="threading_files/colorschememapping.xml">
<!--[if gte mso 9]><xml>
 <w:WordDocument>
  <w:SpellingState>Clean</w:SpellingState>
  <w:GrammarState>Clean</w:GrammarState>
  <w:TrackMoves>false</w:TrackMoves>
  <w:TrackFormatting/>
  <w:ValidateAgainstSchemas/>
  <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
  <w:IgnoreMixedContent>false</w:IgnoreMixedContent>
  <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
  <w:DoNotPromoteQF/>
  <w:LidThemeOther>EN-GB</w:LidThemeOther>
  <w:LidThemeAsian>X-NONE</w:LidThemeAsian>
  <w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
  <w:Compatibility>
   <w:BreakWrappedTables/>
   <w:SnapToGridInCell/>
   <w:WrapTextWithPunct/>
   <w:UseAsianBreakRules/>
   <w:DontGrowAutofit/>
   <w:SplitPgBreakAndParaMark/>
   <w:DontVertAlignCellWithSp/>
   <w:DontBreakConstrainedForcedTables/>
   <w:DontVertAlignInTxbx/>
   <w:Word11KerningPairs/>
   <w:CachedColBalance/>
  </w:Compatibility>
  <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
  <m:mathPr>
   <m:mathFont m:val="Cambria Math"/>
   <m:brkBin m:val="before"/>
   <m:brkBinSub m:val="&#45;-"/>
   <m:smallFrac m:val="off"/>
   <m:dispDef/>
   <m:lMargin m:val="0"/>
   <m:rMargin m:val="0"/>
   <m:defJc m:val="centerGroup"/>
   <m:wrapIndent m:val="1440"/>
   <m:intLim m:val="subSup"/>
   <m:naryLim m:val="undOvr"/>
  </m:mathPr></w:WordDocument>
</xml><![endif]--><!--[if gte mso 9]><xml>
 <w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true"
  DefSemiHidden="true" DefQFormat="false" DefPriority="99"
  LatentStyleCount="267">
  <w:LsdException Locked="false" Priority="0" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Normal"/>
  <w:LsdException Locked="false" Priority="9" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="heading 1"/>
  <w:LsdException Locked="false" Priority="9" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="heading 2"/>
  <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3"/>
  <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4"/>
  <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5"/>
  <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6"/>
  <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7"/>
  <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8"/>
  <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9"/>
  <w:LsdException Locked="false" Priority="39" Name="toc 1"/>
  <w:LsdException Locked="false" Priority="39" Name="toc 2"/>
  <w:LsdException Locked="false" Priority="39" Name="toc 3"/>
  <w:LsdException Locked="false" Priority="39" Name="toc 4"/>
  <w:LsdException Locked="false" Priority="39" Name="toc 5"/>
  <w:LsdException Locked="false" Priority="39" Name="toc 6"/>
  <w:LsdException Locked="false" Priority="39" Name="toc 7"/>
  <w:LsdException Locked="false" Priority="39" Name="toc 8"/>
  <w:LsdException Locked="false" Priority="39" Name="toc 9"/>
  <w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption"/>
  <w:LsdException Locked="false" Priority="10" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Title"/>
  <w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font"/>
  <w:LsdException Locked="false" Priority="11" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Subtitle"/>
  <w:LsdException Locked="false" Priority="22" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Strong"/>
  <w:LsdException Locked="false" Priority="20" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Emphasis"/>
  <w:LsdException Locked="false" Priority="59" SemiHidden="false"
   UnhideWhenUsed="false" Name="Table Grid"/>
  <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text"/>
  <w:LsdException Locked="false" Priority="1" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="No Spacing"/>
  <w:LsdException Locked="false" Priority="60" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Shading"/>
  <w:LsdException Locked="false" Priority="61" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light List"/>
  <w:LsdException Locked="false" Priority="62" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Grid"/>
  <w:LsdException Locked="false" Priority="63" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 1"/>
  <w:LsdException Locked="false" Priority="64" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 2"/>
  <w:LsdException Locked="false" Priority="65" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 1"/>
  <w:LsdException Locked="false" Priority="66" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 2"/>
  <w:LsdException Locked="false" Priority="67" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 1"/>
  <w:LsdException Locked="false" Priority="68" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 2"/>
  <w:LsdException Locked="false" Priority="69" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 3"/>
  <w:LsdException Locked="false" Priority="70" SemiHidden="false"
   UnhideWhenUsed="false" Name="Dark List"/>
  <w:LsdException Locked="false" Priority="71" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Shading"/>
  <w:LsdException Locked="false" Priority="72" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful List"/>
  <w:LsdException Locked="false" Priority="73" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Grid"/>
  <w:LsdException Locked="false" Priority="60" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Shading Accent 1"/>
  <w:LsdException Locked="false" Priority="61" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light List Accent 1"/>
  <w:LsdException Locked="false" Priority="62" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Grid Accent 1"/>
  <w:LsdException Locked="false" Priority="63" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1"/>
  <w:LsdException Locked="false" Priority="64" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1"/>
  <w:LsdException Locked="false" Priority="65" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 1 Accent 1"/>
  <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision"/>
  <w:LsdException Locked="false" Priority="34" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="List Paragraph"/>
  <w:LsdException Locked="false" Priority="29" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Quote"/>
  <w:LsdException Locked="false" Priority="30" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Intense Quote"/>
  <w:LsdException Locked="false" Priority="66" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 2 Accent 1"/>
  <w:LsdException Locked="false" Priority="67" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1"/>
  <w:LsdException Locked="false" Priority="68" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1"/>
  <w:LsdException Locked="false" Priority="69" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1"/>
  <w:LsdException Locked="false" Priority="70" SemiHidden="false"
   UnhideWhenUsed="false" Name="Dark List Accent 1"/>
  <w:LsdException Locked="false" Priority="71" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Shading Accent 1"/>
  <w:LsdException Locked="false" Priority="72" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful List Accent 1"/>
  <w:LsdException Locked="false" Priority="73" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Grid Accent 1"/>
  <w:LsdException Locked="false" Priority="60" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Shading Accent 2"/>
  <w:LsdException Locked="false" Priority="61" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light List Accent 2"/>
  <w:LsdException Locked="false" Priority="62" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Grid Accent 2"/>
  <w:LsdException Locked="false" Priority="63" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2"/>
  <w:LsdException Locked="false" Priority="64" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2"/>
  <w:LsdException Locked="false" Priority="65" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 1 Accent 2"/>
  <w:LsdException Locked="false" Priority="66" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 2 Accent 2"/>
  <w:LsdException Locked="false" Priority="67" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2"/>
  <w:LsdException Locked="false" Priority="68" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2"/>
  <w:LsdException Locked="false" Priority="69" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2"/>
  <w:LsdException Locked="false" Priority="70" SemiHidden="false"
   UnhideWhenUsed="false" Name="Dark List Accent 2"/>
  <w:LsdException Locked="false" Priority="71" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Shading Accent 2"/>
  <w:LsdException Locked="false" Priority="72" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful List Accent 2"/>
  <w:LsdException Locked="false" Priority="73" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Grid Accent 2"/>
  <w:LsdException Locked="false" Priority="60" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Shading Accent 3"/>
  <w:LsdException Locked="false" Priority="61" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light List Accent 3"/>
  <w:LsdException Locked="false" Priority="62" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Grid Accent 3"/>
  <w:LsdException Locked="false" Priority="63" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3"/>
  <w:LsdException Locked="false" Priority="64" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3"/>
  <w:LsdException Locked="false" Priority="65" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 1 Accent 3"/>
  <w:LsdException Locked="false" Priority="66" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 2 Accent 3"/>
  <w:LsdException Locked="false" Priority="67" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3"/>
  <w:LsdException Locked="false" Priority="68" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3"/>
  <w:LsdException Locked="false" Priority="69" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3"/>
  <w:LsdException Locked="false" Priority="70" SemiHidden="false"
   UnhideWhenUsed="false" Name="Dark List Accent 3"/>
  <w:LsdException Locked="false" Priority="71" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Shading Accent 3"/>
  <w:LsdException Locked="false" Priority="72" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful List Accent 3"/>
  <w:LsdException Locked="false" Priority="73" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Grid Accent 3"/>
  <w:LsdException Locked="false" Priority="60" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Shading Accent 4"/>
  <w:LsdException Locked="false" Priority="61" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light List Accent 4"/>
  <w:LsdException Locked="false" Priority="62" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Grid Accent 4"/>
  <w:LsdException Locked="false" Priority="63" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4"/>
  <w:LsdException Locked="false" Priority="64" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4"/>
  <w:LsdException Locked="false" Priority="65" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 1 Accent 4"/>
  <w:LsdException Locked="false" Priority="66" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 2 Accent 4"/>
  <w:LsdException Locked="false" Priority="67" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4"/>
  <w:LsdException Locked="false" Priority="68" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4"/>
  <w:LsdException Locked="false" Priority="69" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4"/>
  <w:LsdException Locked="false" Priority="70" SemiHidden="false"
   UnhideWhenUsed="false" Name="Dark List Accent 4"/>
  <w:LsdException Locked="false" Priority="71" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Shading Accent 4"/>
  <w:LsdException Locked="false" Priority="72" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful List Accent 4"/>
  <w:LsdException Locked="false" Priority="73" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Grid Accent 4"/>
  <w:LsdException Locked="false" Priority="60" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Shading Accent 5"/>
  <w:LsdException Locked="false" Priority="61" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light List Accent 5"/>
  <w:LsdException Locked="false" Priority="62" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Grid Accent 5"/>
  <w:LsdException Locked="false" Priority="63" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5"/>
  <w:LsdException Locked="false" Priority="64" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5"/>
  <w:LsdException Locked="false" Priority="65" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 1 Accent 5"/>
  <w:LsdException Locked="false" Priority="66" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 2 Accent 5"/>
  <w:LsdException Locked="false" Priority="67" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5"/>
  <w:LsdException Locked="false" Priority="68" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5"/>
  <w:LsdException Locked="false" Priority="69" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5"/>
  <w:LsdException Locked="false" Priority="70" SemiHidden="false"
   UnhideWhenUsed="false" Name="Dark List Accent 5"/>
  <w:LsdException Locked="false" Priority="71" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Shading Accent 5"/>
  <w:LsdException Locked="false" Priority="72" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful List Accent 5"/>
  <w:LsdException Locked="false" Priority="73" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Grid Accent 5"/>
  <w:LsdException Locked="false" Priority="60" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Shading Accent 6"/>
  <w:LsdException Locked="false" Priority="61" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light List Accent 6"/>
  <w:LsdException Locked="false" Priority="62" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Grid Accent 6"/>
  <w:LsdException Locked="false" Priority="63" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6"/>
  <w:LsdException Locked="false" Priority="64" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6"/>
  <w:LsdException Locked="false" Priority="65" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 1 Accent 6"/>
  <w:LsdException Locked="false" Priority="66" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 2 Accent 6"/>
  <w:LsdException Locked="false" Priority="67" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6"/>
  <w:LsdException Locked="false" Priority="68" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6"/>
  <w:LsdException Locked="false" Priority="69" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6"/>
  <w:LsdException Locked="false" Priority="70" SemiHidden="false"
   UnhideWhenUsed="false" Name="Dark List Accent 6"/>
  <w:LsdException Locked="false" Priority="71" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Shading Accent 6"/>
  <w:LsdException Locked="false" Priority="72" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful List Accent 6"/>
  <w:LsdException Locked="false" Priority="73" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Grid Accent 6"/>
  <w:LsdException Locked="false" Priority="19" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis"/>
  <w:LsdException Locked="false" Priority="21" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis"/>
  <w:LsdException Locked="false" Priority="31" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference"/>
  <w:LsdException Locked="false" Priority="32" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Intense Reference"/>
  <w:LsdException Locked="false" Priority="33" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Book Title"/>
  <w:LsdException Locked="false" Priority="37" Name="Bibliography"/>
  <w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading"/>
 </w:LatentStyles>
</xml><![endif]-->
<style>
<!--
 /* Font Definitions */
 @font-face
	{font-family:Wingdings;
	panose-1:5 0 0 0 0 0 0 0 0 0;
	mso-font-charset:2;
	mso-generic-font-family:auto;
	mso-font-pitch:variable;
	mso-font-signature:0 268435456 0 0 -2147483648 0;}
@font-face
	{font-family:"Cambria Math";
	panose-1:2 4 5 3 5 4 6 3 2 4;
	mso-font-charset:0;
	mso-generic-font-family:roman;
	mso-font-pitch:variable;
	mso-font-signature:-1610611985 1107304683 0 0 159 0;}
@font-face
	{font-family:Cambria;
	panose-1:2 4 5 3 5 4 6 3 2 4;
	mso-font-charset:0;
	mso-generic-font-family:roman;
	mso-font-pitch:variable;
	mso-font-signature:-1610611985 1073741899 0 0 159 0;}
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;
	mso-font-charset:0;
	mso-generic-font-family:swiss;
	mso-font-pitch:variable;
	mso-font-signature:-1610611985 1073750139 0 0 159 0;}
@font-face
	{font-family:Consolas;
	panose-1:2 11 6 9 2 2 4 3 2 4;
	mso-font-charset:0;
	mso-generic-font-family:modern;
	mso-font-pitch:fixed;
	mso-font-signature:-1610611985 1073750091 0 0 159 0;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{mso-style-unhide:no;
	mso-style-qformat:yes;
	mso-style-parent:"";
	margin:0cm;
	margin-bottom:.0001pt;
	mso-pagination:widow-orphan;
	font-size:12.0pt;
	font-family:"Calibri","sans-serif";
	mso-ascii-font-family:Calibri;
	mso-ascii-theme-font:minor-latin;
	mso-fareast-font-family:"Times New Roman";
	mso-fareast-theme-font:minor-fareast;
	mso-hansi-font-family:Calibri;
	mso-hansi-theme-font:minor-latin;
	mso-bidi-font-family:"Times New Roman";}
h1
	{mso-style-priority:9;
	mso-style-unhide:no;
	mso-style-qformat:yes;
	mso-style-link:"Heading 1 Char";
	mso-margin-top-alt:auto;
	margin-right:0cm;
	mso-margin-bottom-alt:auto;
	margin-left:0cm;
	mso-pagination:widow-orphan;
	mso-outline-level:1;
	font-size:24.0pt;
	font-family:"Cambria","serif";
	mso-ascii-font-family:Cambria;
	mso-ascii-theme-font:major-latin;
	mso-fareast-font-family:"Times New Roman";
	mso-fareast-theme-font:minor-fareast;
	mso-hansi-font-family:Cambria;
	mso-hansi-theme-font:major-latin;
	font-weight:bold;}
h2
	{mso-style-priority:9;
	mso-style-unhide:no;
	mso-style-qformat:yes;
	mso-style-link:"Heading 2 Char";
	mso-margin-top-alt:auto;
	margin-right:0cm;
	mso-margin-bottom-alt:auto;
	margin-left:0cm;
	mso-pagination:widow-orphan;
	mso-outline-level:2;
	font-size:18.0pt;
	font-family:"Calibri","sans-serif";
	mso-ascii-font-family:Calibri;
	mso-ascii-theme-font:minor-latin;
	mso-fareast-font-family:"Times New Roman";
	mso-fareast-theme-font:minor-fareast;
	mso-hansi-font-family:Calibri;
	mso-hansi-theme-font:minor-latin;
	font-weight:bold;}
p
	{mso-style-noshow:yes;
	mso-style-priority:99;
	mso-margin-top-alt:auto;
	margin-right:0cm;
	mso-margin-bottom-alt:auto;
	margin-left:0cm;
	mso-pagination:widow-orphan;
	font-size:12.0pt;
	font-family:"Calibri","sans-serif";
	mso-ascii-font-family:Calibri;
	mso-ascii-theme-font:minor-latin;
	mso-fareast-font-family:"Times New Roman";
	mso-fareast-theme-font:minor-fareast;
	mso-hansi-font-family:Calibri;
	mso-hansi-theme-font:minor-latin;
	mso-bidi-font-family:"Times New Roman";}
pre
	{mso-style-noshow:yes;
	mso-style-priority:99;
	mso-style-link:"HTML Preformatted Char";
	margin:0cm;
	margin-bottom:.0001pt;
	mso-pagination:widow-orphan;
	tab-stops:45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;
	font-size:10.0pt;
	font-family:"Courier New";
	mso-fareast-font-family:"Times New Roman";
	mso-fareast-theme-font:minor-fareast;}
span.Heading1Char
	{mso-style-name:"Heading 1 Char";
	mso-style-priority:9;
	mso-style-unhide:no;
	mso-style-locked:yes;
	mso-style-link:"Heading 1";
	mso-ansi-font-size:24.0pt;
	mso-bidi-font-size:24.0pt;
	font-family:"Cambria","serif";
	mso-ascii-font-family:Cambria;
	mso-ascii-theme-font:major-latin;
	mso-fareast-font-family:"Times New Roman";
	mso-fareast-theme-font:minor-fareast;
	mso-hansi-font-family:Cambria;
	mso-hansi-theme-font:major-latin;
	mso-font-kerning:18.0pt;
	font-weight:bold;}
span.Heading2Char
	{mso-style-name:"Heading 2 Char";
	mso-style-noshow:yes;
	mso-style-priority:9;
	mso-style-unhide:no;
	mso-style-locked:yes;
	mso-style-link:"Heading 2";
	mso-ansi-font-size:13.0pt;
	mso-bidi-font-size:13.0pt;
	font-family:"Cambria","serif";
	mso-ascii-font-family:Cambria;
	mso-ascii-theme-font:major-latin;
	mso-fareast-font-family:"Times New Roman";
	mso-fareast-theme-font:major-fareast;
	mso-hansi-font-family:Cambria;
	mso-hansi-theme-font:major-latin;
	mso-bidi-font-family:"Times New Roman";
	mso-bidi-theme-font:major-bidi;
	color:#4F81BD;
	mso-themecolor:accent1;
	font-weight:bold;}
span.HTMLPreformattedChar
	{mso-style-name:"HTML Preformatted Char";
	mso-style-noshow:yes;
	mso-style-priority:99;
	mso-style-unhide:no;
	mso-style-locked:yes;
	mso-style-link:"HTML Preformatted";
	font-family:Consolas;
	mso-ascii-font-family:Consolas;
	mso-fareast-font-family:"Times New Roman";
	mso-fareast-theme-font:minor-fareast;
	mso-hansi-font-family:Consolas;}
span.SpellE
	{mso-style-name:"";
	mso-spl-e:yes;}
span.GramE
	{mso-style-name:"";
	mso-gram-e:yes;}
.MsoChpDefault
	{mso-style-type:export-only;
	mso-default-props:yes;
	font-size:10.0pt;
	mso-ansi-font-size:10.0pt;
	mso-bidi-font-size:10.0pt;}
@page Section1
	{size:595.3pt 841.9pt;
	margin:72.0pt 72.0pt 72.0pt 72.0pt;
	mso-header-margin:35.4pt;
	mso-footer-margin:35.4pt;
	mso-paper-source:0;}
div.Section1
	{page:Section1;}
 /* List Definitions */
 @list l0
	{mso-list-id:482703332;
	mso-list-template-ids:-1821867934;}
@list l0:level1
	{mso-level-number-format:bullet;
	mso-level-text:\F0B7;
	mso-level-tab-stop:36.0pt;
	mso-level-number-position:left;
	text-indent:-18.0pt;
	mso-ansi-font-size:10.0pt;
	font-family:Symbol;}
@list l1
	{mso-list-id:814682730;
	mso-list-template-ids:1254109184;}
@list l1:level1
	{mso-level-number-format:bullet;
	mso-level-text:\F0B7;
	mso-level-tab-stop:36.0pt;
	mso-level-number-position:left;
	text-indent:-18.0pt;
	mso-ansi-font-size:10.0pt;
	font-family:Symbol;}
@list l2
	{mso-list-id:1505509969;
	mso-list-template-ids:-1148660726;}
@list l2:level1
	{mso-level-number-format:bullet;
	mso-level-text:\F0B7;
	mso-level-tab-stop:36.0pt;
	mso-level-number-position:left;
	text-indent:-18.0pt;
	mso-ansi-font-size:10.0pt;
	font-family:Symbol;}
@list l3
	{mso-list-id:1951087283;
	mso-list-template-ids:1818548752;}
@list l4
	{mso-list-id:2132286371;
	mso-list-template-ids:-763972152;}
@list l4:level1
	{mso-level-number-format:bullet;
	mso-level-text:\F0B7;
	mso-level-tab-stop:36.0pt;
	mso-level-number-position:left;
	text-indent:-18.0pt;
	mso-ansi-font-size:10.0pt;
	font-family:Symbol;}
ol
	{margin-bottom:0cm;}
ul
	{margin-bottom:0cm;}
-->
</style>
<!--[if gte mso 10]>
<style>
 /* Style Definitions */
 table.MsoNormalTable
	{mso-style-name:"Table Normal";
	mso-tstyle-rowband-size:0;
	mso-tstyle-colband-size:0;
	mso-style-noshow:yes;
	mso-style-priority:99;
	mso-style-qformat:yes;
	mso-style-parent:"";
	mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
	mso-para-margin:0cm;
	mso-para-margin-bottom:.0001pt;
	mso-pagination:widow-orphan;
	font-size:10.0pt;
	font-family:"Times New Roman","serif";}
</style>
<![endif]--><!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext="edit" spidmax="2050"/>
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext="edit">
  <o:idmap v:ext="edit" data="1"/>
 </o:shapelayout></xml><![endif]-->
</head>

<body lang=EN-GB link=blue vlink=purple style='tab-interval:36.0pt'>

<div class=Section1>

<h1><span style='mso-fareast-font-family:"Times New Roman"'>Multi-threaded
programming<o:p></o:p></span></h1>

<p>A multi-threaded program allows several &quot;threads of execution&quot; to
run simultaneously.&nbsp; This means that the program is executing in several
places at the same time!&nbsp; If you haven't encountered threads before, then
you are probably worried that this sounds like a recipe for disaster.&nbsp; In
fact it usually is.</p>

<p>There are three common reasons for doing this:</p>

<ol start=1 type=1>
 <li class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
     mso-list:l3 level1 lfo1;tab-stops:list 36.0pt'><span style='mso-fareast-font-family:
     "Times New Roman"'>Logically there are several things happening
     concurrently, and it's the easiest way to express the logic of the
     program.<o:p></o:p></span></li>
 <li class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
     mso-list:l3 level1 lfo1;tab-stops:list 36.0pt'><span style='mso-fareast-font-family:
     "Times New Roman"'>For performance reasons, running things simultaneously
     is faster on multiple CPU cores.<o:p></o:p></span></li>
 <li class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
     mso-list:l3 level1 lfo1;tab-stops:list 36.0pt'><span style='mso-fareast-font-family:
     "Times New Roman"'>To avoid blocking operations such as IO or
     time-consuming computations, <span class=GramE>which would interfere with
     the normal operation of the program, such as keeping a GUI responsive.</span><o:p></o:p></span></li>
</ol>

<p>There are many problems with multi-threaded programming.&nbsp; When
different threads try to modify the same data at exactly the same time, the
program could corrupt the data, or enter a <i>race condition</i> where the precise
sequence of operations changes the outcome of the program.&nbsp; You could also
have a situation where the threads are all waiting on the same thing so the
program becomes &quot;starved&quot;.&nbsp; And you could get a situation where
all threads are waiting on each other - which <span class=GramE>is</span>
called a <i>deadlock</i>.</p>

<p>C++Script is designed to make it easy and safe to write multi-threaded
programs.&nbsp; C++Script uses an &quot;apartment-threaded&quot; model, which
basically creates a different &quot;apartment&quot; or heap per thread.&nbsp; A
garbage collector runs independently in each thread.&nbsp; A thread runs in
just one apartment at a time.&nbsp; When a thread yields (for example accessing
a different apartment, waiting on a <span class=SpellE>mutex</span>, condition,
or sleeping etc), then it releases its current apartment so other threads can access
the data in the apartment. This guarantees that <span class=SpellE><i>var</i></span>
is <span class=SpellE>threadsafe</span>, whilst still allows threads to access
each others' data in a controlled manner. &nbsp;You can still get race
conditions but your program won't crash.</p>

<h2><span style='mso-fareast-font-family:"Times New Roman"'>Creating threads<o:p></o:p></span></h2>

<p>A thread is created by the <span class=GramE><i>thread(</i></span><i>)</i>
function which runs the given <span class=SpellE>functor</span> in a new
thread.&nbsp; The following example (<a href="thread1.cpp">thread1.cpp</a>)
creates&nbsp;<span class=SpellE>functors</span> using <span class=GramE><i>bind(</i></span><i>)</i>,
which when executed writes out the numbers in the range.&nbsp; However the <span
class=GramE><i>thread(</i></span><i>)</i> function creates a new thread and
executes this in a different thread, and the <span class=SpellE><i>script_main</i></span><i>()</i>
function waits for one second (using <i>sleep()</i>) whilst the&nbsp;threads do
their work.</p>

<blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><pre>#include &lt;<span
class=SpellE>cppscript</span>&gt;</pre><pre><o:p>&nbsp;</o:p></pre><pre><span
class=GramE>void</span> <span class=SpellE>thread_fn</span>(<span class=SpellE>var</span> r)</pre><pre>{</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=SpellE><span class=GramE>foreach</span></span><span class=GramE>(</span><span
class=SpellE>i,r</span>) <span class=SpellE>writeln</span>(<span class=SpellE>i</span>);</pre><pre>}</pre><pre><o:p>&nbsp;</o:p></pre><pre><span
class=SpellE><span class=GramE>var</span></span> <span class=SpellE>script_main</span>(<span
class=SpellE>var</span>)</pre><pre>{</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=GramE>thread(</span> bind( <span class=SpellE>thread_fn</span>, range(0,100) ) );</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=GramE>thread(</span> bind( <span class=SpellE>thread_fn</span>, range(100,200) ) );</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=GramE>sleep(</span>1);</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=GramE>return</span> 0;</pre><pre>}</pre></blockquote>

<h2><span style='mso-fareast-font-family:"Times New Roman"'>Synchronising
threads<o:p></o:p></span></h2>

<p>A thread is an object which contains a &quot;<i>join&quot;</i> method.
&nbsp;Calling this method waits for the thread to <span class=GramE>finish,</span>
and returns the&nbsp;exit value of the thread. &nbsp;The exit value can be any <span
class=SpellE><i>var</i></span> type and it isn't limited to integers. &nbsp;In
this respect, <i>&quot;join&quot;</i> is much more like a future. &nbsp;e.g. (<a
href="join.cpp">join.cpp</a>)</p>

<blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><pre>#include &lt;<span
class=SpellE>cppscript</span>&gt;</pre><pre><o:p>&nbsp;</o:p></pre><pre><span
lang=SV style='mso-ansi-language:SV'>var sum_members(var data)<o:p></o:p></span></pre><pre><span
lang=SV style='mso-ansi-language:SV'>{<o:p></o:p></span></pre><pre><span
lang=SV style='mso-ansi-language:SV'><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span>var total = 0;<o:p></o:p></span></pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=SpellE><span class=GramE>foreach</span></span><span class=GramE>(</span> <span
class=SpellE>i</span>, data ) total += <span class=SpellE>i</span>;</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=GramE>return</span> total;</pre><pre>}</pre><pre><o:p>&nbsp;</o:p></pre><pre><span
class=SpellE><span class=GramE>var</span></span> <span class=SpellE>script_main</span>(<span
class=SpellE>var</span>)</pre><pre>{</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=SpellE><span class=GramE>var</span></span> thread1 = thread(bind(<span
class=SpellE>sum_members</span>, range(1,10000)));</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=SpellE><span class=GramE>var</span></span> thread2 = thread(bind(<span
class=SpellE>sum_members</span>, range(10001,20000)));</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=SpellE><span class=GramE>var</span></span> total = thread1[&quot;join&quot;]() + thread2[&quot;join&quot;]();</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=SpellE><span class=GramE>writeln</span></span><span class=GramE>(</span> &quot;The total is &quot; + total );</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=GramE>return</span> 0;</pre><pre>}</pre></blockquote>

<p>If a thread throws an exception, then the exception is re-thrown by the <i>&quot;join&quot;</i>
method.</p>

<p>There are also functions for <span class=SpellE>mutexes</span> and
events.&nbsp; You should avoid these functions and prefer <a
href="#_Message_queues_">message queues</a>.</p>

<h2><a name="_Message_queues_"></a><a name=MessageQueues></a><span
style='mso-fareast-font-family:"Times New Roman"'>Message queues <o:p></o:p></span></h2>

<p>A good way for threads to communicate is using <i>message queues</i>.
&nbsp;A message queue is a sequence of values, and a number of&nbsp;<i>producer
threads</i> push values onto the queue, and a number of&nbsp;<i>consumer
threads</i> pop values from the queue.</p>

<p>A message queue is created using the&nbsp;<span class=GramE><i>queue(</i></span><i>)</i>
function, which has the following methods:</p>

<ul type=disc>
 <li class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
     mso-list:l2 level1 lfo3;tab-stops:list 36.0pt'><i><span style='mso-fareast-font-family:
     "Times New Roman"'>void &quot;post&quot;(<span class=SpellE>var</span>
     value)</span></i><span style='mso-fareast-font-family:"Times New Roman"'>
     - pushes a value onto the queue<o:p></o:p></span></li>
 <li class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
     mso-list:l2 level1 lfo3;tab-stops:list 36.0pt'><span class=SpellE><i><span
     style='mso-fareast-font-family:"Times New Roman"'>var</span></i></span><i><span
     style='mso-fareast-font-family:"Times New Roman"'> &quot;pop&quot;()</span></i><span
     style='mso-fareast-font-family:"Times New Roman"'> - pops a value from the
     queue, or waits until a value becomes available<o:p></o:p></span></li>
 <li class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
     mso-list:l2 level1 lfo3;tab-stops:list 36.0pt'><span class=SpellE><i><span
     style='mso-fareast-font-family:"Times New Roman"'>var</span></i></span><i><span
     style='mso-fareast-font-family:"Times New Roman"'> &quot;wait&quot;()</span></i><span
     style='mso-fareast-font-family:"Times New Roman"'> - waits for all items
     to be consumed from the queue<o:p></o:p></span></li>
</ul>

<p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><i><span
style='mso-fareast-font-family:"Times New Roman"'><o:p>&nbsp;</o:p></span></i></p>

<p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span
class=GramE><i><span style='mso-fareast-font-family:"Times New Roman"'>queue(</span></i></span><i><span
style='mso-fareast-font-family:"Times New Roman"'>)</span></i><span
style='mso-fareast-font-family:"Times New Roman"'> implements a FIFO stack. <o:p></o:p></span></p>

<h2><span style='mso-fareast-font-family:"Times New Roman"'>Thread pools<o:p></o:p></span></h2>

<p class=MsoNormal><span style='mso-fareast-font-family:"Times New Roman"'>A
thread pool is a message queue with a number of consumer threads. &nbsp;Each
consumer is a <span class=SpellE>functor</span> (executed in a separate
thread), which receives and processes messages. &nbsp;A thread pool is
created&nbsp;by the <span class=SpellE><i>message_<span class=GramE>queue</span></i></span><span
class=GramE><i>(</i></span><i>)</i> function, which accepts a <span
class=SpellE>functor</span>, and optionally a number of threads (default=1).<o:p></o:p></span></p>

<ul type=disc>
 <li class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
     mso-list:l4 level1 lfo4;tab-stops:list 36.0pt'><span class=SpellE><i><span
     style='mso-fareast-font-family:"Times New Roman"'>var</span></i></span><i><span
     style='mso-fareast-font-family:"Times New Roman"'> <span class=SpellE>message_queue</span>(const
     <span class=SpellE>var</span> &amp; <span class=SpellE>functor</span>,
     const <span class=SpellE>var</span> &amp; threads=1)</span></i><span
     style='mso-fareast-font-family:"Times New Roman"'> - creates a message
     queue and consumer threads<o:p></o:p></span></li>
</ul>

<p>A <span class=SpellE>message_queue</span> is a type of queue, with the
following methods:</p>

<ul type=disc>
 <li class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
     mso-list:l1 level1 lfo5;tab-stops:list 36.0pt'><i><span style='mso-fareast-font-family:
     "Times New Roman"'>void &quot;post&quot;(<span class=SpellE>var</span>
     value)</span></i><span style='mso-fareast-font-family:"Times New Roman"'>
     - pushes a value onto the queue<o:p></o:p></span></li>
 <li class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
     mso-list:l1 level1 lfo5;tab-stops:list 36.0pt'><i><span style='mso-fareast-font-family:
     "Times New Roman"'>void &quot;wait&quot;()</span></i><span
     style='mso-fareast-font-family:"Times New Roman"'> - waits for all current
     messages to be processed<o:p></o:p></span></li>
 <li class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
     mso-list:l1 level1 lfo5;tab-stops:list 36.0pt'><i><span style='mso-fareast-font-family:
     "Times New Roman"'>void &quot;close&quot;()</span></i><span
     style='mso-fareast-font-family:"Times New Roman"'> - stops all of the
     threads in a controlled manner<o:p></o:p></span></li>
</ul>

<p>It is good practise to <i>&quot;close&quot;()</i> the queue in order to tidy
up <span class=SpellE><span class=GramE>an</span></span> running threads (which
may otherwise not&nbsp;stop when the program exits).</p>

<p>The following example (<a href="queue.cpp">queue.cpp</a>) creates a message
queue with a single consumer thread, which receives a sequence of numbers.
&nbsp;The consumer (the function <span class=SpellE><i>counter_add</i></span>),
adds each number to the total (<span class=GramE><i>counter[</i></span><i>&quot;total&quot;]</i>).
&nbsp;The += operator is actually atomic so multiple consumer threads could
work safely. &nbsp;As an added refinement, the <i>&quot;post&quot;</i> method
of the queue is assigned to the <i>&quot;<span class=SpellE>queued_add</span>&quot;</i>
member of the counter, implementing an active method.</p>

<blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><pre>#include &lt;<span
class=SpellE>cppscript</span>&gt;</pre><pre><o:p>&nbsp;</o:p></pre><pre><span
class=GramE>void</span> <span class=SpellE>counter_add</span>(<span
class=SpellE>var</span> counter, <span class=SpellE>var</span> value)</pre><pre>{</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=GramE>counter[</span>&quot;total&quot;] += value;</pre><pre>}</pre><pre><o:p>&nbsp;</o:p></pre><pre><span
class=SpellE><span class=GramE>var</span></span> counter()</pre><pre>{</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=GramE>return</span> object(&quot;counter&quot;).extend</pre><pre><span
style='mso-tab-count:2'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>(&quot;<span
class=GramE>total</span>&quot;, 0)</pre><pre><span style='mso-tab-count:2'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>(&quot;<span
class=GramE>add</span>&quot;, <span class=SpellE>counter_add</span>);</pre><pre>}</pre><pre><o:p>&nbsp;</o:p></pre><pre><span
class=SpellE><span class=GramE>var</span></span> <span class=SpellE>script_main</span>(<span
class=SpellE>var</span>)</pre><pre>{</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span>// Set up</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=SpellE><span class=GramE>var</span></span> counter1 = counter();</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=SpellE><span class=GramE>var</span></span> queue1 = <span class=SpellE>message_queue</span>(counter1[&quot;add&quot;]);</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=GramE>finally(</span>queue1[&quot;close&quot;]);<span style='mso-tab-count:
1'>&nbsp;&nbsp; </span>// Join the thread</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=GramE>counter1[</span>&quot;<span class=SpellE>queued_add</span>&quot;] = queue1[&quot;post&quot;];</pre><pre><o:p>&nbsp;</o:p></pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span>// Queue the data</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=SpellE><span class=GramE>foreach</span></span><span class=GramE>(</span><span
class=SpellE>i</span>, range(1,1000)) counter1[&quot;<span class=SpellE>queued_add</span>&quot;](<span
class=SpellE>i</span>);</pre><pre><o:p>&nbsp;</o:p></pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span>// Wait for the queue to process all items</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=GramE>queue1[</span>&quot;wait&quot;]();</pre><pre><o:p>&nbsp;</o:p></pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span>// Finish</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=SpellE><span class=GramE>writeln</span></span><span class=GramE>(</span> &quot;The total is &quot; + counter1[&quot;total&quot;] );</pre><pre><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span><span
class=GramE>return</span> 0;</pre><pre>}</pre></blockquote>

</div>

</body>

</html>
